define(['angularAMD'], function (angularAMD) {
    angularAMD.service('B0223Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog) {
            this.init = function(options) {

                function joint_production_get(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.joint_production_get',
                        content : content,
                        msg : 'B0223.msg.joint_production_get',
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }

                function joint_production_process(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.joint_production_process',
                        content : content,
                        msg : 'B0223.msg.joint_production_process',
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }
                var openDialog = function(data){
                    $mdDialog.dialog(
                        'module/moduleB/program/B02/B0223/B0223.html',
                        function (dialog) {
                            return {
                                plotItem: data,
                                css_url:'module/moduleB/program/B02/B0223/B0223.css',
                                List_PageConfig: {
                                    page: 0,
                                    page_count: 25
                                }, 
                                List_filter: {},
                                back: function () {
                                    var newItem = dialog.plotItem;
                                    dialog.hide();
                                    if(options.close){
                                        options.close(newItem);
                                    }
                                },
                                confirm: function(){
                                    if(dialog.selectItem){
                                        if(options.parent != 'B0401' && options.parent != 'B1301'){
                                            var content = {
                                                plot_no: dialog.plotItem.plot_no,
                                                plot_stamp: dialog.plotItem.plot_stamp,
                                                status: options.plotItem.status||'11',
                                                joint_item_no: dialog.selectItem.joint_item_no,
                                                joint_item_unit_no: dialog.selectItem.joint_item_unit_no
                                            };
                                            joint_production_process(content, function(data){
                                                $rootScope.showToast($filter('translate')('B0223.msg.success'));
                                                var newItem = dialog.selectItem;
                                                dialog.hide();
                                                if(options.feedback)
                                                    options.feedback(newItem);
                                            });
                                        } else {
                                            var newItem = dialog.selectItem;
                                            dialog.hide();
                                            if(options.feedback)
                                                options.feedback(newItem);
                                        }
                                    } else {
                                        $rootScope.showAlert($filter('translate')('common.msg.plz_select_one'));
                                    }
                                },
                                getPages: function(list, PageConfig, filterItem) {
                                    if (list) {
                                        var temp_list = list.filter(function (item, index, array) {
                                            var isOK = true,
                                                keys = Object.keys(filterItem);
                                            for (var i = 0; i < keys.length; i++) {
                                                var key = keys[i];
                                                if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                                                    if (item[key].indexOf(filterItem[key]) == -1) {
                                                        isOK = false;
                                                        break;
                                                    }
                                                }
                                            }
                                            return isOK;
                                        });
                                        if (PageConfig) {
                                            if (temp_list.length != 0) {
                                                PageConfig.total_page = Math.ceil(temp_list.length / PageConfig.page_count);
                    
                                                if (PageConfig.page >= PageConfig.total_page) {
                                                    PageConfig.page = 0;
                                                }
                                                var index = PageConfig.page * PageConfig.page_count;
                                                return temp_list.slice(index, index + PageConfig.page_count);
                                            } else {
                                                PageConfig.total_page = 1;
                                                PageConfig.page = 0;
                                            }
                                        } else {
                                            return temp_list;
                                        }
                                    } else {
                                        return list;
                                    }
                                }
                            };
                        }
                    );
                };
                var content = {
                    plot_no : options.plotItem.plot_no,
                    op_no : options.plotItem.op_no,
                    status : options.plotItem.status||'11',
                    do_flag: options.do_flag
                };
                joint_production_get(content, function(data){
                    openDialog(data);
                });
            };
        }]);
});
